package org.platform.processor.diagnose;


import com.mc.mic.core.log.bus.BusLogService;
import org.platform.dao.mapper.FdChannelEquMapper;
import org.platform.dao.mapper.FdDiagnoseTestStepMapper;
import org.platform.dao.mapper.FdDiagnosetaskMapper;
import org.platform.dao.mapper.FdWorkSchInfoMapper;
import org.platform.mo.bussmo.task.TaskReport;
import org.platform.mo.db.FdChannelEqu;
import org.platform.mo.db.FdDiagnoseTestStep;
import org.platform.mo.db.FdDiagnosetask;
import org.platform.mo.db.FdWorkSchInfo;
import org.platform.tool.helper.ResourceHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 诊断日志/步骤处理类
 *
 * @author FengJie
 * @date 2018/7/19
 */
@Service
public class DiagnoseRecordProcessor {
    @Autowired
    private FdDiagnosetaskMapper taskMapper;
    @Autowired
    private FdWorkSchInfoMapper workSchInfoMapper;
    @Autowired
    private FdChannelEquMapper channelEquMapper;
    @Autowired
    private FdDiagnoseTestStepMapper testStepMapper;
    @Autowired
    private ResourceHelper resource;
    @Autowired
    private BusLogService logService;

    /**
     * 保存任务步骤信息
     *
     * @param schInfo
     */
    public void saveWorkSchInfo(TaskReport.WorkSchInfo schInfo) {
        FdWorkSchInfo info = new FdWorkSchInfo();
        BeanUtils.copyProperties(schInfo, info);
        String id = info.getTaskSerialNumber() + "_" + info.getJobTaskID();
        info.setId(id);
        workSchInfoMapper.insert(info);

        List<TaskReport.channelEqu> channelInfoList = schInfo.getChannelinfoList();
        for (TaskReport.channelEqu channelEqu : channelInfoList) {
            FdChannelEqu fdChannelEqu = new FdChannelEqu();
            BeanUtils.copyProperties(channelEqu, fdChannelEqu);
            fdChannelEqu.setId(id + "_" + channelEqu.getChlIndex());
            fdChannelEqu.setWorkSchInfoId(id);
            channelEquMapper.insert(fdChannelEqu);
        }
    }

    /**
     * @param diagnoseId 诊断ID (测试编号)
     * @param option     操作
     */
    public void saveDiagnoseRecord(String diagnoseId, String option) {
        FdDiagnosetask task = taskMapper.selectByPrimaryKey(diagnoseId);
        saveDiagnoseRecord(task, option);
    }

    /**
     * 调用框架api保存诊断日志
     */
    public void saveDiagnoseRecord(FdDiagnosetask task, String option) {
        logService.normal(option, BusLogService.LogType.FD, resource.getStationName(), task.getDiagnoseid(), task.getDeviceid());

        ////保存诊断日志
        //FdDiagnoseRecord record = new FdDiagnoseRecord();
        //record.setId(ComUtils.getUUID());
        //record.setDiagnoseid(task.getDiagnoseid());
        //record.setDeviceid(task.getDeviceid());
        //record.setHappendtime(System.currentTimeMillis());
        //record.setStation(resource.getStationName());
        //record.setContent(option);
        //record.setType(0);
        //recordMapper.insert(record);

        //推动到客户端
        //mqLog.log(new LogBo<>(LogType.DIAGNOSE_RECORD, new DiagnoseRecordMqBO(record)));
    }

    /**
     * 保存测试步骤
     *
     * @param testStepList 测试步骤
     */
    public void saveTestStep(List<FdDiagnoseTestStep> testStepList) {
        testStepList.forEach(step ->
                testStepMapper.insert(step)
        );
    }
}
